﻿Imports System.Data.OleDb
Public Class details
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim sql As String = "INSERT INTO comments (author,title,commentbody,cars_fk_id) VALUES(@a1,@a2,@a3,@a4)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@a1", txtbox_author.Text)
        cmd.Parameters.AddWithValue("@a2", txtbox_title.Text)
        cmd.Parameters.AddWithValue("@a3", txtbox_commentbody.Text)
        cmd.Parameters.AddWithValue("@a4", Request.QueryString("ID"))
        conn.Open()

        If (cmd.ExecuteNonQuery()) Then
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
            MsgBox("comment saved")
            Response.Redirect("details.aspx?ID=" & Request.QueryString("ID"))

        Else
            MsgBox("Sorry, your comment wasnt saved. chek and be sure everything is okay!!..")
            txtbox_author.Focus()
            Return
        End If
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Dim sql As String = "INSERT INTO cartags (car_fk_id,tag_fk_id) VALUES(@a1,@a2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        conn.Open()

        For Each cb As ListItem In cb_tags.Items
            If cb.Selected = True Then
                'add this to the database
                Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@a1", Request.QueryString("ID"))
                cmd.Parameters.AddWithValue("@a2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

    End Sub

    Protected Sub btn_newtag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_newtag.Click

        Dim sql As String = "INSERT INTO tags (tagname) VALUES (@a1)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("mercedesbenzconnectionstring").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@a1", tb_newtag.Text)
        cmd.ExecuteNonQuery()

        Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID from tags ORDER BY ID DESC", conn)
        Dim dr As OleDbDataReader = cmd2.ExecuteReader()
        dr.Read()
        Dim highestid = dr(0)

        Dim associate As String = "INSERT INTO cartags (car_fk_id,tag_fk_id) VALUES (@a1,@a2)"
        Dim cmd3 As OleDbCommand = New OleDbCommand(associate, conn)
        cmd3.Parameters.AddWithValue("@a1", Request.QueryString("ID"))
        cmd3.Parameters.AddWithValue("@a2", highestid)
        cmd3.ExecuteNonQuery()

    End Sub
End Class